package action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {

	private static final long serialVersionUID = -8116415738542545210L;

	public String doUpload() throws IOException {
		System.out.println(upload + ":" + uploadContentType + ":"
				+ uploadFileName);
		// 取得服务器的本地路径
		String savePath = ServletActionContext.getServletContext().getRealPath(
				"/upload/" + this.uploadFileName);
		System.out.println(savePath);
		FileInputStream fis = new FileInputStream(upload);
		FileOutputStream fos = new FileOutputStream(savePath);
		IOUtils.copy(fis, fos);
		fos.flush();
		fis.close();
		fis.close();
		return SUCCESS;
	}

	/**
	 * upload是上传文件标签的name属性的值
	 */
	private File upload;
	private String uploadContentType;
	private String uploadFileName;

	/**
	 * 获取 upload是上传文件标签的name属性的值
	 * 
	 * @return upload是上传文件标签的name属性的值
	 */
	public File getUpload() {
		return upload;
	}

	/**
	 * 设置 upload是上传文件标签的name属性的值
	 * 
	 * @param upload
	 *            upload是上传文件标签的name属性的值
	 */
	public void setUpload(File upload) {
		this.upload = upload;
	}

	/**
	 * 获取 uploadContentType
	 * 
	 * @return uploadContentType
	 */
	public String getUploadContentType() {
		return uploadContentType;
	}

	/**
	 * 设置 uploadContentType
	 * 
	 * @param uploadContentType
	 *            uploadContentType
	 */
	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	/**
	 * 获取 uploadFilename
	 * 
	 * @return uploadFilename
	 */
	public String getUploadFileName() {
		return uploadFileName;
	}

	/**
	 * 设置 uploadFilename
	 * 
	 * @param uploadFilename
	 *            uploadFilename
	 */
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

}
